package ee.serge.client.content.cp;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import ee.serge.client.AlertBox;
import ee.serge.client.Move;
import ee.serge.service.Service;
import ee.serge.util.AdminContent;
import ee.serge.util.ClickLabel;
import ee.serge.util.Loader;
import ee.serge.util.SimpleCallback;
import ee.serge.util.URLData;
import ee.serge.util.bootstrap.BButton;
import ee.serge.util.bootstrap.BButtonBar;
import ee.serge.util.bootstrap.BTable;
import ee.serge.shared.Loan;

public class LoansPanelContent extends AdminContent {

   private static final String NAME = "cp/loans";
   
   public static String getName() {
      return NAME;
   }
   
   public static URLData getURLData() {
      URLData data = new URLData();
      data.setName(NAME);
      return data;
   }
   
   public static void show() {
      Service.getControlPanelService().getLoans(new SimpleCallback<List<Loan>>() {
         @Override
         public void success(List<Loan> result) {
            Move.get().setContent(new LoansPanelContent(result));
            Loader.hide();
         }
      }); 
   }
      
   public LoansPanelContent(List<Loan> result) {
      makePanel(result);
      Loader.hide();
   }
   
   private void makePanel(List<Loan> loans) {
          
      setHeader("Loans", "Add, edit and remove");
      String[] headers = {"#", "Name", "Min. loan", "Max. loan", "APR", "Period", "", ""};
      BTable grid = new BTable(loans.size(), headers, BTable.ZEBRA_STRIPED);
      for (int i = 0; i < loans.size(); i++) {
         Loan loan = loans.get(i);
         grid.setText(i, 0, String.valueOf(i+1));
         grid.setText(i, 1, loan.getName());
         grid.setText(i, 2, String.valueOf(loan.getMinLoan()));
         grid.setText(i, 3, String.valueOf(loan.getMaxLoan()));
         grid.setText(i, 4, String.valueOf(loan.getAPR()));
         grid.setText(i, 5, String.valueOf(loan.getPeriod()));
         grid.setWidget(i, 6, new ClickLabel("Edit", new EditHandler(loan.getKey())));
         if (!loan.isActive()) {
            grid.getRowFormatter().addStyleName(i, "inactive");
         }
         grid.setWidget(i, 7, new ClickLabel("Remove", new RemoveHandler(loan.getKey())));
      }
      
      add(grid);
      BButtonBar buttonBar = new BButtonBar();
      buttonBar.add(new BButton("Add loan", BButton.PRIMARY, new AddLoanHandler()));
      add(buttonBar);
   }
   
   private static class EditHandler implements ClickHandler {
      private String key;
      public EditHandler(String key) {
         this.key = key;
      }
      @Override
      public void onClick(ClickEvent event) {
         LoanEditPopup.show(key);
      }
      
   }
   
   private static class RemoveHandler implements ClickHandler {
	      private String key;
	      public RemoveHandler(String key) {
	         this.key = key;
	      }
	      @Override
	      public void onClick(ClickEvent event) {
	         Service.getControlPanelService().removeLoan(key, new SimpleCallback<Void>() {
					@Override
               public void success(Void result) {
						Move.get().refresh();
	               AlertBox.showSuccess("Loan removed");
               }
				});
	      }
	      
	   }
   
   private static class AddLoanHandler implements ClickHandler {
      @Override
      public void onClick(ClickEvent event) {
         LoanEditPopup.showAddNew();
      }
      
   }

   
}
